<?php
include_once("functions/db_manipulate.php");

if (!init()) {
  redirect("?action=login");
}

$fliesResultSet = getLastFlies();
$archiveResultSet = getAllFlies();
$lastIds = getLastFliesId();

$arrayId[] = null;
$it = 0;

while ($id = mysql_fetch_assoc($lastIds)) {
  $arrayId[$it] = $id['id'];
  $it++;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=<?= getMessageByKey("default.charset") ?>"/>
  <title><?= getMessageByKey("flies.title") ?></title>
  <link rel="stylesheet" type="text/css" href="css/navigation.css">
  <link rel="stylesheet" type="text/css" href="css/menu.css">
  <link rel="stylesheet" type="text/css" href="css/common.css">
  <link rel="stylesheet" type="text/css" href="css/flies.css">
  <script src="resources/jquery.js"></script>
  <script language="JavaScript">
    function customFormSubmit() {
      $('#maximized_navigation').show();
      $('#commentform').submit();
    }
  </script>

  <script language="JavaScript">
    function switchDiv(div1, div2) {
      $('#' + div1).hide('slow')
      $('#' + div2).show('slow')
    }
  </script>
</head>
<body>

<?php
include_once("menu.php");
?>

<div class="maincommon">

  <div id="flyarchivebutton" onclick="$('#flyarchive').toggle('slow')">
    <?= getMessageByKey('flies.archive')?>
  </div>

  <div id="flyarchive">


    <div class="flydiv" style="font-weight: bold; font-style: normal">
      <div class="cell">
        <?= getMessageByKey('general.date')?>
      </div>
      <div class="cell">
        <?= getMessageByKey('general.title')?>
      </div>
      <div class="cell">
        <?= getMessageByKey('general.author')?>
      </div>
    </div>

    <?php
    while ($archiveFly = mysql_fetch_assoc($archiveResultSet)) {
      ?>
      <div class="flydiv" onclick="location.href = '?action=showfly&fid=<?= $archiveFly['id'] ?>'">
        <div class="cell">
          <?= $archiveFly['date'] ?>
        </div>
        <div class="cell">
          <?= getTextPart($archiveFly['header'], 10) ?>
        </div>
        <div class="cell">
          <?= $archiveFly['author'] ?>
        </div>
      </div>
    <?php
    }
    ?>
  </div>

  <div class="fliesheader">
    <?= getMessageByKey("flies.header") ?>
  </div>


  <?php

  $it = 0;

  while ($fly = mysql_fetch_assoc($fliesResultSet)) {
    $next = $it < 5 ? $arrayId[$it + 1] : $arrayId[0];
    $previous = $it > 0 ? $arrayId[$it - 1] : $arrayId[5];

    $it++;
    ?>
    <div class="fly" id="fly<?= $fly['id'] ?>">
      <div class="flybutton" onclick="switchDiv('fly<?= $fly['id'] ?>', 'fly<?= $previous ?>')">
        <?= getMessageByKey('button.flies.archive.previous')?>
      </div>
      <div class="flyimage">
        <img src="<?= \controller\Config::getPropertyByKey('directory.preview.fly.topic') . $fly['path'] ?>" onclick="location.href = '?action=showfly&fid=<?= $fly['id'] ?>'">
      </div>
      <div class="flybutton" onclick="switchDiv('fly<?= $fly['id'] ?>', 'fly<?= $next ?>')">
        <?= getMessageByKey('button.flies.archive.next')?>
      </div>
    </div>
  <?php } ?>


  <form enctype="multipart/form-data" accept-charset="UTF-8" action="?action=flyhand" method="post" id="commentform">

    <div class="newcomment">
      <div class="info">
        <?= getMessageByKey("flies.new") ?>
      </div>
      <textarea name="header" id="imageheader" rows="1"
                placeholder="<?= getMessageByKey("flies.new.header.placeholder") ?>"
                required="true"></textarea>
      <textarea name="legend" rows="11"
                placeholder="<?= getMessageByKey("flies.new.content.placeholder") ?>"></textarea>

      <input type="hidden" name="MAX_FILE_SIZE" value="25000000"/>

      <div class="buttonsdiv">


        <div class="fileselect">
          <input name="image" required="true" type="file" accept=".jpg, .png, .jpeg, .gif" class="realfileinput" id="realfileinput1"
                 onmouseover="$('#fakefileinput1').css('background-color', 'rgb(73, 67, 255)')"
                 onmouseout="$('#fakefileinput1').css('background-color', 'rgba(0,0,0,1)')"/>

          <div class="button" id="fakefileinput1">
            <?= getMessageByKey("button.general.fly.image") ?>
          </div>
        </div>

        <div class="fileselect">
          <input style="width: 142px;" name="images[]" required="true" multiple type="file" accept=".jpg, .png, .jpeg, .gif" class="realfileinput" id="realfileinput2"
                 onmouseover="$('#fakefileinput2').css('background-color', 'rgb(73, 67, 255)')"
                 onmouseout="$('#fakefileinput2').css('background-color', 'rgba(0,0,0,1)')"/>

          <div class="button" style="position: relative; left: -146px"  id="fakefileinput2">
            <?= getMessageByKey("button.general.fly.images.others") ?>
          </div>
        </div>


        <div class="button" onclick="customFormSubmit()">
          <?= getMessageByKey("button.general.add") ?>
        </div>
      </div>
    </div>
  </form>


</div>

<?php
include_once("navigation.php");
?>

</body>
</html>